﻿using System;
using System.ComponentModel;
using ScrumFactory.Composition.ViewModel;
using ScrumFactory.Composition;
using ScrumFactory.Services;
using System.Collections.Generic;


namespace ScrumFactory.ProjectsList.ViewModel {


    /// <summary>
    /// Create Project View Model.
    /// </summary>
    public class CreateProjectViewModel : ICreateProjectViewModel, IPanelViewModel, IWindowViewModel {

        private string projectName;
        private string clientName;
        private string description;

        private IEventAggregator aggregator;
        private IBackgroundExecutor executor;
        private IProjectsService projectService;

        /// <summary>
        /// Initializes a new instance of the <see cref="CreateProjectViewModel"/> class.
        /// </summary>
        /// <param name="backgroundExecutor">The background executor.</param>
        /// <param name="eventAggregator">The event aggregator.</param>
        /// <param name="projectService">The project service.</param>
        public CreateProjectViewModel(
            IBackgroundExecutor backgroundExecutor,            
            IEventAggregator eventAggregator,
            IProjectsService projectService) {

                this.aggregator = eventAggregator;
                this.executor = backgroundExecutor;
                this.projectService = projectService;

                View = new CreateProject();
                View.Model = this;
            
                MoveWindowCommand = new DelegateCommand(() => { View.DragMove(); });
                CancelCommand = new DelegateCommand(() => { View.Close(); });

                CreateProjectCommand = new DelegateCommand(CanCreateProject, CreateProject);
        }

        /// <summary>
        /// Determines whether this instance can create project.
        /// </summary>
        /// <returns>
        /// 	<c>true</c> if this instance can create project; otherwise, <c>false</c>.
        /// </returns>
        private bool CanCreateProject() {
            if (String.IsNullOrWhiteSpace(ProjectName) || String.IsNullOrWhiteSpace(ClientName) || String.IsNullOrWhiteSpace(Description))
                return false;
            return true;
        }

        /// <summary>
        /// Creates the project.
        /// </summary>
        private void CreateProject() {
         
            // creates the new project
            NewProject = new Project();
            NewProject.ProjectUId = Guid.NewGuid().ToString();

            NewProject.ProjectName = ProjectName;
            NewProject.ClientName = ClientName;
            NewProject.Description = Description;

            // create the deafult roles
            Role defaultRoleSM = new Role() {
                RoleUId = System.Guid.NewGuid().ToString(),
                RoleName = Properties.Resources.Scrum_Master_Role,
                RoleDescription = Properties.Resources.Scrum_Master_Role_description,
                ProjectUId = NewProject.ProjectUId };            

            Role defaultRoleTEAM = new Role() {
                RoleUId = System.Guid.NewGuid().ToString(),
                RoleName = Properties.Resources.Team_Role,
                RoleDescription = Properties.Resources.Team_Role_description,
                ProjectUId = NewProject.ProjectUId };

            NewProject.Roles = new List<Role>() { defaultRoleSM, defaultRoleTEAM };
            

            // saves it
            projectService.CreateProject(NewProject);

            // checks if it was saved with success
            //NewProject = projectService.GetProject(NewProject.ProjectUId);
            
            // close the window
            View.Close();

        }

        /// <summary>
        /// Gets or sets the view.
        /// </summary>
        /// <value>The view.</value>
        public ICreateProjectView View { get; set; }
                    
        #region IPanelViewModel Members

        /// <summary>
        /// Gets the name of the panel.
        /// </summary>
        /// <value>The name of the panel.</value>
        public string PanelName {
            get { return Properties.Resources.ResourceManager.GetString("Create_new_project"); }
        }

        /// <summary>
        /// Gets the view.
        /// </summary>
        /// <value>The view.</value>
        object IPanelViewModel.View {
            get { return this.View; }
        }

        /// <summary>
        /// Gets the panel display order when it is displayed at the view.
        /// </summary>
        /// <value>The panel display order.</value>
        public int PanelDisplayOrder {
            get { return 0; }
        }

        /// <summary>
        /// Gets the panel Unique id.
        /// </summary>
        /// <value>The panel Unique id.</value>
        public string PanelUId {
            get { return "CREATE_PROJECT_PANEL"; }
        }

        /// <summary>
        /// Gets the panel placement.
        /// </summary>
        /// <value>The panel placement.</value>
        public PanelPlacements PanelPlacement {
            get { return PanelPlacements.Popup; }
        }

        #endregion

        #region IWindowViewModel Members

        /// <summary>
        /// Gets the view.
        /// </summary>
        /// <value>The view.</value>
        object IWindowViewModel.View {
            get { return this.View; }
            set { this.View = (ICreateProjectView) value; }
        }    

        /// <summary>
        /// Shows this instance.
        /// </summary>
        public void Show() {
            View.Show();
        }

        /// <summary>
        /// Closes this instance.
        /// </summary>
        public void Close() {
            View.Close();
        }

        /// <summary>
        /// Gets the move window command.
        /// </summary>
        /// <value>The move window command.</value>
        public System.Windows.Input.ICommand MoveWindowCommand { get; private set; }

        /// <summary>
        /// Gets the close window command.
        /// </summary>
        /// <value>The close window command.</value>
        public System.Windows.Input.ICommand CloseWindowCommand { get; private set; }

        /// <summary>
        /// Gets the minimize window command.
        /// </summary>
        /// <value>The minimize window command.</value>
        public System.Windows.Input.ICommand MinimizeWindowCommand { get; private set; }

        /// <summary>
        /// Gets the maximize window command.
        /// </summary>
        /// <value>The maximize window command.</value>
        public System.Windows.Input.ICommand MaximizeWindowCommand { get; private set; }

        #endregion

        #region ICreateProjectViewModel Members

        /// <summary>
        /// Gets the new project.
        /// </summary>
        /// <value>The new project.</value>
        public Project NewProject { get; private set; }

        /// <summary>
        /// Gets or sets the name of the project.
        /// </summary>
        /// <value>The name of the project.</value>
        public string ProjectName {
            get {
                return projectName;
            }
            set {
                projectName = value;
                ((DelegateCommand)CreateProjectCommand).NotifyCanExecuteChanged();
            }
        }

        /// <summary>
        /// Gets or sets the name of the client.
        /// </summary>
        /// <value>The name of the client.</value>
        public string ClientName {
            get {
                return clientName;
            }
            set {
                clientName = value;
                ((DelegateCommand)CreateProjectCommand).NotifyCanExecuteChanged();
            }
        }

        /// <summary>
        /// Gets or sets the description.
        /// </summary>
        /// <value>The description.</value>
        public string Description {
            get {
                return description;
            }
            set {
                description = value;
                ((DelegateCommand)CreateProjectCommand).NotifyCanExecuteChanged();
            }
        }

        /// <summary>
        /// Gets the create project command.
        /// </summary>
        /// <value>The create project command.</value>
        public System.Windows.Input.ICommand CreateProjectCommand { get; private set; }

        /// <summary>
        /// Gets the cancel command.
        /// </summary>
        /// <value>The cancel command.</value>
        public System.Windows.Input.ICommand CancelCommand { get; private set; }

        #endregion

      
    }
}
